<template>
  <div class="container">
    <div class="top">
      <el-input style="width:200px" type="text" v-model="search.username" placeholder="请输入管理员用户名/名称"></el-input>
      <el-select v-model="search.acc_type" placeholder="请选择账号类型">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
      >查询</el-button>
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
      >清空</el-button>
       <el-button
      class="add"
      size="small"
      type="primary"
      icon="el-icon-circle-plus"
      @click="handleAddAdmin()"
    >新增管理员</el-button>
    </div>
   
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <el-table-column prop="admin_id" label="id" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" align="center"></el-table-column>
      <el-table-column prop="nickname" label="管理员名称" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
      <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
      <el-table-column label="操作" width="450" align="center">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-lock" type="warning" @click="password(scope)">重置密码</el-button>
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope)">编辑</el-button>
          <el-button
            size="mini"
            v-if="scope.row.acc_type == 3"
            icon="el-icon-edit-outline"
            type="success"
            @click="bindingSupplier(scope)"
          >绑定供应商</el-button>

          <el-button
            v-if="scope.row.islock == 1"
            size="mini"
            icon="el-icon-unlock"
            type="warning"
            @click="unlock(scope)"
          >解锁</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
    <!-- dialog -->
    <adminDialog :dialog="dialog" :formData="formData" @upData="getData"></adminDialog>
    <!-- 绑定供应商dialog -->
    <bindingSupplierDialog :dialogs="dialogs"></bindingSupplierDialog>
  </div>
</template>
<script>
import AdminDialog from "@/components/permission/adminDialog";
import BindingSupplierDialog from "@/components/permission/bindingSupplierDialog";
import Pagination from "@/components/pagination";
import {
  SelectAdminList,
  delAdminInfo,
  unfreeze,
  resetAdminPassword
} from "@/api/permission/adminlist";
export default {
  components: {
    pagination: Pagination,
    adminDialog: AdminDialog,
    bindingSupplierDialog: BindingSupplierDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        admin_id: ""
      },

      search: {
        username: "",
        acc_type: ""
      },
      options: [
        {
          value: 0,
          label: "平台账号"
        },
        {
          value: 1,
          label: "供应商账号"
        },
        {
          value: 2,
          label: "店铺账号"
        },
        {
          value: 3,
          label: "采购账号"
        },
        {
          value: 4,
          label: "财务账号"
        },
        {
          value: 5,
          label: "商超采购员账号"
        },
        {
          value: 6,
          label: "商超供应商账号"
        },
        {
          value: 7,
          label: "商超财务账号"
        },
        {
          value: 8,
          label: "挑单账号"
        }
      ],

      dialogs: {
        centerDialogVisible: false,
        id: ""
      },
      formData: {}
    };
  },
  computed: {
    routesData() {
      return this.routes;
    }
  },
  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        username: this.search.username,
        acc_type: this.search.acc_type
      };
      SelectAdminList(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {});
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData()
    },
    //清空
    empty() {
      this.search ={
         username: '',
        acc_type: ''
      }
    },
    // 添加管理员
    handleAddAdmin() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.admin_id = "";
    },
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.admin_id = scope.row.admin_id;
    },
    //绑定供应商
    bindingSupplier(scope) {
      console.log(scope);
      this.dialogs.centerDialogVisible = true;
      this.dialogs.id = scope.row.admin_id;
    },
    //解锁
    unlock(scope) {
      let data = {
        id: scope.row.admin_id
      };
      this.$confirm("是否解锁该管理员?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          unfreeze(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消解锁"
          });
        });
    },
    //删除管理员
    handleDelete(scope) {
      let data = {
        admin_id: scope.row.admin_id
      };
      console.log(scope);
      this.$confirm("是否删除该管理员?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delAdminInfo(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //重置密码
    password(scope) {
      let data = {
        admin_id: scope.row.admin_id
      };
      console.log(scope);
      this.$confirm("是否重置密码?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          resetAdminPassword(data).then(res => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: res.msg
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
</style>